package com.xcdh.ittools.config;

import com.alibaba.druid.pool.DruidDataSource;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.xcdh.ittools.provider.JdbcDataSourceProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(DynamicDataSourceProperties.class)
public class DynamicDataSourceConfiguration {

    private final DynamicDataSourceProperties properties;

    public DynamicDataSourceConfiguration(DynamicDataSourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public JdbcDataSourceProvider dynamicDataSourceProvider(){
        DataSourceProperty dataSourceProperty = properties.getDatasource().get(properties.getPrimary());
        return new JdbcDataSourceProvider(dataSourceProperty) ;
    }
}
